package io.yuanqi.hgtcshop.controller;


import com.alibaba.fastjson.JSON;
import io.yuanqi.hgtcshop.entity.enums.ResultCode;
import io.yuanqi.hgtcshop.entity.param.DFPayParam;
import io.yuanqi.hgtcshop.entity.param.OrderParam;
import io.yuanqi.hgtcshop.entity.param.PayParam;
import io.yuanqi.hgtcshop.entity.vo.OrderVO;
import io.yuanqi.hgtcshop.entity.vo.ResultMessage;
import io.yuanqi.hgtcshop.entity.vo.UserNameAndFaceVO;
import io.yuanqi.hgtcshop.exception.ServiceException;
import io.yuanqi.hgtcshop.service.OrderService;
import io.yuanqi.hgtcshop.service.PayService;
import io.yuanqi.hgtcshop.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 买家端，订单接口
 *
 * @author Chopper
 * @since 2020/11/16 10:04 下午
 */
@Slf4j
@RestController
//@Api(tags = "买家端，订单接口")
@RequestMapping("/buyer/payment/cashier/pay")
public class payController {


    @Autowired
    private PayService payService;

    @PostMapping
    public ResultMessage<Object> crateTrade(PayParam payParam) {
        try {
            log.info(".crateTrade orderSn:{}", payParam);
            //读取选中的列表
            return ResultUtil.data(this.payService.jsApiPay(payParam));
        } catch (ServiceException se) {
            log.info(se.getMsg(), se);
            throw se;
        } catch (Exception e) {
            log.error(ResultCode.ORDER_ERROR.message(), e);
            throw e;
        }
    }


    @PostMapping("/df")
    public ResultMessage<Object> crateDfTrade(DFPayParam payParam) {
        try {
            log.info(".crateDfTrade请求参数为:{}", JSON.toJSONString(payParam));
            //读取选中的列表
            return ResultUtil.data(this.payService.jsDfApiPay(payParam));
        } catch (ServiceException se) {
            log.info(se.getMsg(), se);
            throw se;
        } catch (Exception e) {
            log.error(ResultCode.ORDER_ERROR.message(), e);
            throw e;
        }
    }

    @RequestMapping(value = "/notify", method = {RequestMethod.GET, RequestMethod.POST})
    public void notify(HttpServletRequest request) throws Exception {
        log.info(".notify 接受到微信回调：request:{}", request);
        payService.verifyNotify(request);
    }


    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Boolean> query(String orderSn) throws Exception {
        log.info(".query orderSn:{}", orderSn);
        return ResultUtil.data(payService.query(orderSn));
    }


}
